//
//  HDShutterView.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/3/31.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

protocol HDShutterViewDelegate:class {
    func didClickShutter()
}

class HDShutterView: UIView {
    
    weak var delegate : HDShutterViewDelegate?
    let inCircleLayer = CAShapeLayer()
    var lineWidth:CGFloat = 0.0
    
    let rect = CGRect(x: 0, y: 0, width: 50, height: 50)
    init() {
        super.init(frame: CGRect.zero)
        
        lineWidth = rect.size.width * 0.12 - 2.5
        setupLayer()
    }
    
    func setupLayer() {
        //边框
        let circleBorder = CAShapeLayer()
        
        circleBorder.frame = rect
        circleBorder.borderWidth = lineWidth
        circleBorder.borderColor = UIColor.white.cgColor
        circleBorder.cornerRadius = rect.size.width / 2
        self.layer.addSublayer(circleBorder)
        
        
        //内部
        inCircleLayer.frame = CGRect(x: self.lineWidth+2, y: self.lineWidth+2, width: rect.size.width-(lineWidth+2)*2, height: rect.size.height-(self.lineWidth+2)*2)
        inCircleLayer.cornerRadius = inCircleLayer.frame.size.width/2
        inCircleLayer.backgroundColor = UIColor.white.cgColor
        self.layer.addSublayer(inCircleLayer)


        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        inCircleLayer.opacity = 0.5
        
        
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        inCircleLayer.opacity = 1
        delegate?.didClickShutter()
    }
    
}
